﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Xml;

using System.Data.SqlClient;
using SpringWaterDataAccess;

namespace client
{
	/// <summary>
	/// Summary description for bloggermain.
	/// </summary>
	public partial class bloggermain : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Repeater Repeater1;
		protected System.Web.UI.WebControls.LinkButton LinkButton2;
		protected System.Web.UI.WebControls.LinkButton LinkButton3;
		protected System.Web.UI.WebControls.LinkButton LinkButton1;
		protected System.Web.UI.WebControls.Repeater Repeater2;
		protected System.Web.UI.WebControls.DataList DataList2;
        protected System.Web.UI.WebControls.GridView GridView1;
		protected int categoryID = 0;

		public static int USER_ATTICLES_PRAYER_DAILY = 169;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // Put user code to initialize the page here
            categoryID = Convert.ToUInt16(Request.QueryString["CategoryID"]);
            HttpCookie nameCookie = Request.Cookies["NickName"];

            LinkButton1.Enabled = nameCookie != null;
            LinkButton2.Enabled = nameCookie != null;
            LinkButton3.Enabled = nameCookie != null;

            if (Request.Browser.Browser == "IE")
            {
                GridView1.Font.Size = System.Web.UI.WebControls.FontUnit.XSmall;
            }
            else
            {
                GridView1.Font.Size = System.Web.UI.WebControls.FontUnit.Small;
            }

            int totalRecords = 0;

            ForumDataAccess da = new ForumDataAccess();
            DataSet blogDS = da.getPosts(0, DateTime.Now.Year, 0, 0, 1, 500,
                out totalRecords, "blog", "Active", categoryID, "CreateDate desc");

            // userid 168 is the special blog for the homepage, filter it
            blogDS.Tables[0].DefaultView.RowFilter = "UserType <> 'column'";


            GridView1.DataSource = blogDS.Tables[0].DefaultView;
            GridView1.DataBind();


            if (!IsPostBack)
            {

                DataList2.DataSource = da.getUsers("christian", "blog");
                DataList2.DataBind();

               
                DataSet topDS = da.getPosts(0, 0, 0, 0, 1, 20,
                    out totalRecords, "blog", "Top", 0, "CreateDate desc");

                // CategoryID 43 indicates not categorized
                //blogDS.Tables[0].DefaultView.RowFilter = "CategoryID <> 43";

                Repeater2.DataSource = topDS.Tables[0].DefaultView;
                Repeater2.DataBind();

                DataSet visitDS = da.getPosts(0, DateTime.Now.Year, 0, 0, 1, 100,
                    out totalRecords, "blog", "Active", 0, "NumberOfVisits desc");
                //visitDS.Tables[0].DefaultView.RowFilter = "CategoryID <> 43";
                visitDS.Tables[0].DefaultView.RowFilter = "UserType <> 'column'";
                //visitDS.Tables[0].DefaultView.RowFilter = "UserID = 93";

                Repeater1.DataSource = visitDS.Tables[0].DefaultView;
                Repeater1.DataBind();

            }
        }

		private string GetPrayerForChinaToday()
		{
			String URLString = "http://www.prayforchina.net/guest.asp";
	
			XmlDocument xmlDocContinents = new XmlDocument();

			XmlNode node = null;
			try 
			{
				xmlDocContinents.Load(URLString);
			
				DateTime today = DateTime.Now;
        
				node = xmlDocContinents.SelectSingleNode("/prayforchina/daily/chinese/items"); 
				//xmlDocContinents.SelectSingleNode("/prayforchina/daily/chinese[date='" + 
				//today.ToString("yyyy-m-d") + "']/items");
			}	
			catch (Exception ex) 
			{
				return "" + ex.Message;
			}


			if (node == null)
				return "";
			else
				return  node.InnerText;
		}  

		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.Repeater1.ItemCommand += new System.Web.UI.WebControls.RepeaterCommandEventHandler(this.Repeater1_ItemCommand);
			this.Load += new System.EventHandler(this.Page_Load);

		}
		#endregion

		private void Repeater1_ItemCommand(object source, System.Web.UI.WebControls.RepeaterCommandEventArgs e)
		{
		
		}

	 

		 

        protected void LinkButton2_Click1(object sender, EventArgs e)
        {
            if (Request.Cookies["Email"] == null)
            {
                return;
            }

            DataSet ds = UserObject.GetUser(Request.Cookies["Email"].Value);
            string UserID = Convert.ToString(ds.Tables[0].Rows[0]["UserID"]);
            Response.Redirect("bloggerinput.aspx?UserID=" + UserID);
		
        }

        protected void LinkButton3_Click1(object sender, EventArgs e)
        {
            if (Request.Cookies["Email"] == null)
            {
                return;
            }

            DataSet ds = UserObject.GetUser(Request.Cookies["Email"].Value);
            string UserID = Convert.ToString(ds.Tables[0].Rows[0]["UserID"]);
            Response.Redirect("bloggerhome.aspx?UserID=" + UserID);
		

        }

        protected void LinkButton1_Click1(object sender, EventArgs e)
        {
            if (Request.Cookies["Email"] == null)
            {
                return;
            }

            DataSet ds = UserObject.GetUser(Request.Cookies["Email"].Value);
            string UserID = Convert.ToString(ds.Tables[0].Rows[0]["UserID"]);
            Response.Redirect("bloggerweb.aspx?UserID=" + UserID);

        }

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            GridView1.DataBind();
        }

       
	}
}  
